package com.credit.aspect;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import com.credit.context.CreditServletContext;
import com.credit.exception.ErrorException;
import com.credit.util.Constants;
import com.credit.util.ParamsMapUtil;
import com.credit.web.basic.bean.ResBean;

import net.sf.json.JSONObject;

@Aspect
public class ControllerAroundAspect {
	private static final Log log = LogFactory.getLog(ControllerAroundAspect.class);
	
	@Around("execution(* com.credit.web.basic.*.*(..))")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {

	    // start stopwatch   �൱����before advice

		Object retVal = null;
		ResBean resBean = null;
		
	    try{
	    	retVal = pjp.proceed();

		    // stop stopwatch    �൱����after advice

	    	resBean = CreditServletContext.getResBean();
	    	
		    //return retVal;
	    }catch(ErrorException e){
	    	e.printStackTrace();
	    	resBean = new ResBean();
	    	resBean.setCode(e.getCode());
	    	resBean.setDesc(e.getMessage());
	    	resBean.setTimestamp(System.currentTimeMillis());
	    }catch(Exception e){
	    	e.printStackTrace();
	    	resBean = new ResBean();
	    	resBean.setCode(Constants.SERVER_OTHER_ERROR_CODE);
	    	resBean.setDesc(Constants.SERVER_OTHER_ERROR_DESC);
	    	resBean.setTimestamp(System.currentTimeMillis());
	    }
	    
	    String result = JSONObject.fromObject(resBean).toString();
	    
	    log.info("返回的结果数据为 [ "+result+" ]");
	    
	    HttpServletResponse response = CreditServletContext.getResponse();
	    response.getWriter().write(result);
	    response.getWriter().flush();
	    response.getWriter().close();
		return retVal;

	  }

}
